package com.xc.disk.enums;

import com.xc.disk.exception.OperateException;
import com.xc.disk.exception.ServerException;

/**
 * <p>项目名称: Stars</p>
 * <p>描述: 故障代码</p>
 * <P>创建人:小程 </p>
 * <P>创建人邮箱: 779943476@qq.com</p>
 * <p>创建时间: 2019/12/10</p>
 *
 * @version v1.0
 */
public enum FailCode {
    //服务器异常
    SERVER_EXCEPTION("sc001", "服务器异常,请联系管理员！"),
    SAFETY_CHAIN_CREATE_FAIL("sc002", "安全链接创建失败！"),
    SAFETY_CHAIN_UPDATE_FAIL("sc003", "安全链接更新失败！"),
    SAFETY_CHAIN_DELETE_FAIL("sc004", "安全链接删除失败！"),

    //操作异常
    SAFETY_CHAIN_NOT_EXIST("oc002", "安全链接信息不存在！"),
    NOT_THE_CREATOR("oc003", "你不是数据的创建者，无法操作！"),
    URL_REPEAT("oc004", "url重复！");

    /**
     * 错误code
     */
    private String code;
    /**
     * 消息
     */
    private String message;

    FailCode() {
    }

    FailCode(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public String getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public OperateException getOperateException() {
        return new OperateException(this.code, this.getMessage());
    }

    public ServerException getServerException() {
        return new ServerException(this.code, this.getMessage());
    }

    public Exception exceptionHandle(String code, String message) {
        if ("o".equals(code.substring(0, 1))) {
            return new OperateException(code, message);
        } else {
            return new ServerException(code, message);
        }
    }
}
